﻿using System;
using Mono.Cecil.Metadata;
using Mono.Cecil.PE;

namespace Mono.Cecil.Cil
{
	// Token: 0x0200012B RID: 299
	public sealed class PortablePdbReader : ISymbolReader, IDisposable
	{
		// Token: 0x170002F3 RID: 755
		// (get) Token: 0x06000B65 RID: 2917 RVA: 0x00033C64 File Offset: 0x00031E64
		private bool IsEmbedded
		{
			get
			{
				return this.reader.image == this.debug_reader.image;
			}
		}

		// Token: 0x06000B66 RID: 2918 RVA: 0x0000607F File Offset: 0x0000427F
		internal PortablePdbReader(Image image, ModuleDefinition module)
		{
			this.image = image;
			this.module = module;
			this.reader = module.reader;
			this.debug_reader = new MetadataReader(image, module, this.reader);
		}

		// Token: 0x06000B67 RID: 2919 RVA: 0x00033C90 File Offset: 0x00031E90
		public ISymbolWriterProvider GetWriterProvider()
		{
			return new PortablePdbWriterProvider();
		}

		// Token: 0x06000B68 RID: 2920 RVA: 0x00033CA8 File Offset: 0x00031EA8
		public bool ProcessDebugHeader(ImageDebugHeader header)
		{
			bool flag = this.image == this.module.Image;
			bool result;
			if (flag)
			{
				result = true;
			}
			else
			{
				foreach (ImageDebugHeaderEntry entry in header.Entries)
				{
					bool flag2 = !PortablePdbReader.IsMatchingEntry(this.image.PdbHeap, entry);
					if (!flag2)
					{
						this.ReadModule();
						return true;
					}
				}
				result = false;
			}
			return result;
		}

		// Token: 0x06000B69 RID: 2921 RVA: 0x00033D1C File Offset: 0x00031F1C
		private static bool IsMatchingEntry(PdbHeap heap, ImageDebugHeaderEntry entry)
		{
			bool flag = entry.Directory.Type != ImageDebugType.CodeView;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				byte[] data = entry.Data;
				bool flag2 = data.Length < 24;
				if (flag2)
				{
					result = false;
				}
				else
				{
					int num = PortablePdbReader.ReadInt32(data, 0);
					bool flag3 = num != 1396986706;
					if (flag3)
					{
						result = false;
					}
					else
					{
						byte[] array = new byte[16];
						Buffer.BlockCopy(data, 4, array, 0, 16);
						Guid a = new Guid(array);
						Buffer.BlockCopy(heap.Id, 0, array, 0, 16);
						Guid b = new Guid(array);
						result = (a == b);
					}
				}
			}
			return result;
		}

		// Token: 0x06000B6A RID: 2922 RVA: 0x00033DC0 File Offset: 0x00031FC0
		private static int ReadInt32(byte[] bytes, int start)
		{
			return (int)bytes[start] | (int)bytes[start + 1] << 8 | (int)bytes[start + 2] << 16 | (int)bytes[start + 3] << 24;
		}

		// Token: 0x06000B6B RID: 2923 RVA: 0x000060B6 File Offset: 0x000042B6
		private void ReadModule()
		{
			this.module.custom_infos = this.debug_reader.GetCustomDebugInformation(this.module);
		}

		// Token: 0x06000B6C RID: 2924 RVA: 0x00033DF0 File Offset: 0x00031FF0
		public MethodDebugInformation Read(MethodDefinition method)
		{
			MethodDebugInformation methodDebugInformation = new MethodDebugInformation(method);
			this.ReadSequencePoints(methodDebugInformation);
			this.ReadScope(methodDebugInformation);
			this.ReadStateMachineKickOffMethod(methodDebugInformation);
			this.ReadCustomDebugInformations(methodDebugInformation);
			return methodDebugInformation;
		}

		// Token: 0x06000B6D RID: 2925 RVA: 0x000060D5 File Offset: 0x000042D5
		private void ReadSequencePoints(MethodDebugInformation method_info)
		{
			method_info.sequence_points = this.debug_reader.ReadSequencePoints(method_info.method);
		}

		// Token: 0x06000B6E RID: 2926 RVA: 0x000060EF File Offset: 0x000042EF
		private void ReadScope(MethodDebugInformation method_info)
		{
			method_info.scope = this.debug_reader.ReadScope(method_info.method);
		}

		// Token: 0x06000B6F RID: 2927 RVA: 0x00006109 File Offset: 0x00004309
		private void ReadStateMachineKickOffMethod(MethodDebugInformation method_info)
		{
			method_info.kickoff_method = this.debug_reader.ReadStateMachineKickoffMethod(method_info.method);
		}

		// Token: 0x06000B70 RID: 2928 RVA: 0x00006123 File Offset: 0x00004323
		private void ReadCustomDebugInformations(MethodDebugInformation info)
		{
			info.method.custom_infos = this.debug_reader.GetCustomDebugInformation(info.method);
		}

		// Token: 0x06000B71 RID: 2929 RVA: 0x00033E2C File Offset: 0x0003202C
		public void Dispose()
		{
			bool isEmbedded = this.IsEmbedded;
			if (!isEmbedded)
			{
				this.image.Dispose();
			}
		}

		// Token: 0x0400070E RID: 1806
		private readonly Image image;

		// Token: 0x0400070F RID: 1807
		private readonly ModuleDefinition module;

		// Token: 0x04000710 RID: 1808
		private readonly MetadataReader reader;

		// Token: 0x04000711 RID: 1809
		private readonly MetadataReader debug_reader;
	}
}
